package com.ihr360.expr.core;

import com.ihr360.expr.ExprBoolean;
import com.ihr360.expr.ExprException;
import com.ihr360.expr.context.IEvaluationContext;

import java.io.Serializable;

public abstract class Expr implements Comparable<Expr>,Serializable{
    public final ExprType type;
    public final boolean evaluatable;

    protected Expr(ExprType type, boolean evaluatable) {
        this.type = type;
        this.evaluatable = evaluatable;
    }

    public boolean isVolatile() {
        return false;
    }

    public void validate() throws ExprException {
    }

    protected Expr eval(Expr expr, IEvaluationContext context)
            throws ExprException {
        if (expr instanceof ExprEvaluatable) {
            return ((ExprEvaluatable) expr).evaluate(context);
        }
        return expr;
    }



    @Override
    public int compareTo(Expr o) {
        return this.toString().compareTo(o.toString());
    }

    public int compareIgnoreCase(Expr another) {
        return this.toString().toLowerCase().compareTo(another.toString().toLowerCase());
    }

    protected ExprBoolean bool(boolean bool) {
        return bool ? ExprBoolean.TRUE : ExprBoolean.FALSE;
    }

}